<?php
class npcController extends  appController{
    public function home()
    {
        $home_list = Model('npc')->getHome();
        $this->assign("home_list", $home_list);
        $this->display("home.html");
    }
    
    public function updateUrl()
    {
         $id = yf_empty($_POST['id'],"请重新选择");
         $info['url'] = yf_empty($_POST['url'],"请填写跳转路径");
         $res = Model("npc")->update($info,array("table"=>"npc_home","id"=>$id));
         if($res)
         {
             $this->showMsg(array("error"=>0,"content"=>"修改成功"));
         }else{
             $this->showMsg(array("error"=>1,"content"=>"修改失败"));
         }
    }
    
    //两会
	public function index()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$list=Model('npc')->listNpc($page);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$this->assign('count',$list['count']);
		$this->assign('page',$page);
			
		$this->display('npc_list.html');
	}
	
	public function editNpc()
	{
		if(isset($_GET['id']))
		{
			$info = Model('npc')->get($_GET['id'],"npc");
			$this->assign('info', $info);
		}
		$this->display("npc_edit.html");
	}
	public function updateNpc()
	{
		$result = array('error'=>0,'content'=>'编辑成功');
		
		$info['title'] = yf_empty($_POST['title'],"标题不可为空");
		$info['img_url'] = yf_empty($_POST['img_url'],"请上传图片");
		$info['content'] = yf_empty($_POST['content'],"描述不可为空");
		$info['des_content'] = yf_empty($_POST['des_content'],"页面内容不可为空");
// 		if(preg_match('/script/', $info['des_content']))
// 		{
// 		    $result['error']=1;
// 		    $result['content']="页面内容不可有javascript内容";
// 		    $this->showMsg($result);
// 		}
		if(!empty($_POST['id']))
		{//编辑
			Model('npc')->update($info,array('table'=>'npc','id'=>$_POST['id']));
		}
		else
		{//新增
			$info['create_time'] = yf_time();
			Model('npc')->insert($info,'npc');
		}
		$this->showMsg($result);
	}
	public function delNpc()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('npc')->del(array("table"=>"npc","id"=>$v));
			}
		}
		else
		{
			$result=Model('npc')->del(array("table"=>"npc","id"=>$id));
		}
		if($result){
			$res = array("error"=>0,"content"=>"删除成功");
		}else{
			$res = array("error"=>1,"content"=>"删除失败");
		}
		print_r(json_encode($res));
	}
	
	public function commentList()
	{
		$m = Model('npc');
		$total['nid'] = $_GET['nid'];
		$total['startTime'] = yf_empty(strtotime($_GET['startTime']),"",true);
		$total['endTime'] = yf_empty(strtotime($_GET['endTime']),"",true);
		$total['nickname'] = yf_empty($_GET['nickname'],"",true);
		$total['comment'] = yf_empty($_GET['comment'],"",true);
		$total['status'] = yf_empty($_GET['status'],"all",true);
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$list=Model('npc')->listComment($page,$total);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$this->assign('count',$list['count']);
		$this->assign('page',$page);
		$this->assign("total", $total);
		$this->display("comment_list.html");
	}
	
	public function updateCommentStatus()
	{
		$info['status'] = yf_empty($_REQUEST['status'],1,true);
		$id = yf_empty($_REQUEST['id'],"请选择审核的内容");
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('npc')->update($info,array("table"=>"npc_comment","id"=>$v));
			}
		}
		else
		{
			$result=Model('npc')->update($info,array("table"=>"npc_comment","id"=>$id));
		}
		if($result){
			$this->showMsg(array("error"=>0,"content"=>"审核修改成功"));
		}else{
			$this->showMsg(array("error"=>1,"content"=>"审核修改失败"));
		}
	}
	
	public function addDigest()
	{
	    $id = isset($_POST['id'])?$_POST['id']:"";
	    $res = Model('npc')->addDigest($id);
	    print_r(json_encode($res));
	}
	
	public function review()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$list=Model('npc')->listCommentOldest($page);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$this->assign('page',$page);
		$timeout = Model('npc')->get_cfg("reviewRefresh");
		$this->assign("timeout", $timeout);
		$this->display('comment_oldest_list.html');
	}
}